package com.system.controller;

import com.system.pojo.SysUser;
import com.system.service.SysUserService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@ResponseBody
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/login")
    public ResultVo<SysUser> login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setPassword(password);
        ResultVo<SysUser> resultVo = new ResultVo<>();
        try {
            resultVo = sysUserService.login(sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setMessage("登陆失败");
        }
        if (resultVo.isSelect()){
            //登陆成功
            session.setAttribute("sysUser", resultVo.getT());
            session.setMaxInactiveInterval(30*60);
            Cookie cookie = new Cookie("user", username);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        System.out.println("返回了");
        return resultVo;
    }

    @RequestMapping(value = "/getUser",method = RequestMethod.GET)
    public ResultVo<SysUser> getUser(HttpSession session,HttpServletResponse response){
        ResultVo<SysUser> resultVo = new ResultVo<>();
        SysUser sysUser =(SysUser) session.getAttribute("sysUser");
        if (sysUser != null){
            resultVo.setSelect(true);
            resultVo.setT(sysUser);
        }else{
            resultVo.setMessage("用户获取失败");
        }
        return resultVo;
    }

    @RequestMapping("/selectAllSaler")
    public ResultVo<SysUser> selectAllSaler(){
        ResultVo<SysUser> resultVo = null;
        try {
            resultVo = sysUserService.selectAllSaler();
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询销售人员失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/saler/insert")
    public ResultVo<SysUser> salerInsert(SysUser sysUser){
        ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
        try {
            resultVo = sysUserService.salerInsert(sysUser);
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("添加销售人员失败");
        }finally {
            return  resultVo;
        }
    }

    @RequestMapping("/selectAll")
    public ResultVo<SysUser> selectAll(){
        ResultVo<SysUser> resultVo = null;
        try {
            resultVo =  sysUserService.selectAll();
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询失败");
        }finally {
            return resultVo;
        }

    }

    @RequestMapping("/update")
    public ResultVo<SysUser> update(SysUser sysUser){
        ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
        System.out.println(sysUser);
        try {
            resultVo =  sysUserService.update(sysUser);
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("更新失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/salerUpdate")
    public ResultVo<SysUser> salerUpdate(SysUser sysUser){
        ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
        try {
            resultVo =  sysUserService.salerUpdate(sysUser);
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("更新失败");
        }finally {
            return resultVo;
        }
    }


    @RequestMapping("/insert")
    public ResultVo<SysUser> insert(SysUser sysUser){
        ResultVo<SysUser> resultVo = null;
        try {
            resultVo =  sysUserService.insert(sysUser);
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("更新失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/logout")
    public ResultVo<SysUser> logout(HttpServletRequest request,HttpServletResponse response){
        ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
        HttpSession session = request.getSession();
        try {
            session.removeAttribute("sysUser");
            resultVo.setDelete(true);
            resultVo.setMessage("注销用户成功");
        } catch (Exception e) {
           e.printStackTrace();
            resultVo.setMessage("注销用户失败");
        }finally {
            return resultVo;
        }
    }


    @RequestMapping("/select/{userId}")
    public ResultVo<SysUser> selectById(@PathVariable Integer userId){
        ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
        try {
            resultVo = sysUserService.selectById(userId);
            resultVo.setSelect(true);
            resultVo.setMessage("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setMessage("查询用户失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/delete/{adminId}")
    public ResultVo<SysUser> delete(@PathVariable Integer adminId){
        ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
        try {
            resultVo = sysUserService.deleteById(adminId);
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("删除失败");
        }finally {
            return resultVo;
        }
    }
}
